﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;
using wt.wine.Domain.Entities;

namespace wt.wine.Application.ManageSkutable.Command
{
    /// <summary>
    /// 修改商品
    /// </summary>
    public class EditSkutableCommand : IRequest<Json<string>>
    {
        public int id { get; set; }
        public string name { get; set; }
        public int price { get; set; }
        /// <summary>
        /// 目前固定值：50
        /// </summary>
        public int unit { get; set; }
        public string remark { get; set; }
    }

    public class EditSkutableCommandHandler : IRequestHandler<EditSkutableCommand, Json<string>>
    {
        private readonly IskutableRepository _iskutableRepository;
        private readonly ICurrentUserService _icurrentuserServices;
        public EditSkutableCommandHandler(IskutableRepository iskutableRepository, ICurrentUserService icurrentuserServices)
        {
            _iskutableRepository = iskutableRepository;
            _icurrentuserServices = icurrentuserServices;
        }
        public async Task<Json<string>> Handle(EditSkutableCommand request, CancellationToken cancellationToken)
        {
            int userid = _icurrentuserServices.GetUserId();
            if (userid <= 0)
            {
                return Json<string>.Failed("请先登录", "");
            }
            else
            {
                if (request.id <= 0)
                {
                    return Json<string>.Failed("请选择需要修改的商品", "");
                }
                else if (string.IsNullOrWhiteSpace(request.name))
                {
                    return Json<string>.Failed("商品名称不能为空", "");
                }
                else if (request.name.Length > 50)
                {
                    return Json<string>.Failed("商品名称不能太长", "");
                }
                else if (request.price <= 0)
                {
                    return Json<string>.Failed("商品价格不能为0", "");
                }
                else if (request.unit <= 0)
                {
                    return Json<string>.Failed("零售价单位不能为0", "");
                }
                else
                {
                    var model = await _iskutableRepository.GetSkutableById(request.id, userid, cancellationToken);
                    if ((model?.id ?? 0) <= 0)
                    {
                        return Json<string>.Failed("请选择需要修改的商品", "");
                    }
                    model.name = request.name;
                    model.price = request.price;
                    model.unit = request.unit;
                    model.userpid = userid;
                    model.updatetime = DateTime.Now;
                    model.remark = request.remark + "";
                    await _iskutableRepository.EditSkutable(model, cancellationToken);
                    return Json<string>.Success("修改成功", "");
                }

            }
        }
    }
}
